Release 10.1A: OpenEdge Data Management:
SQL Development
Types of integrity constraints
SQL provides four types of integrity constraints:
SQL allows you to specify an integrity constraint, and to refer to that constraint in other SQL statements. The database assigns a constraint name if you do not specify one.
Example 5–11 shows the assignment of table constraint
prim_constron tablesupplier_item. You specify a constraint name with theCONSTRAINTkeyword.
Check constraints
The values you enter for a row must be valid so that the data in the database is consistent. For example, the city names you enter into the supplier table must correspond to one of the cities where the suppliers are located. The database checks to ensure that each value corresponds to one of the valid city names. You achieve these validations by specifying check constraints during the definition of the table schema. Use check constraints when you want to restrict a column to a set of valid values.
Example 5–12 shows how to specify a check constraint on the supplier table. In this example, the city column is defined with a check constraint to verify that values for city are in the set of
NEW YORK,BOSTON,DALLAS, orMANCHESTER. ThisCREATEstatement does not use theCONSTRAINTkeyword in the table definition. The system assigns a constraint name.
A check constraint on a table specifies a condition on the column values of a row in that table. Whenever you issue an
INSERTorUPDATEstatement against a table containing check constraints, the database validates the column values. TheINSERTorUPDATEoperation is completed only after successful validation.You can specify a check constraint at either the column level or the table level.
Column-level check constraints
In an application, you might decide to check a particular column for valid data whenever you attempt to
INSERTorUPDATEvalues for that column. For example, you design your database to disallow suppliers from a place called Toxic Island. Use a column-level check constraint for this type of validation.In Example 5–13, there is a column-level check constraint on the city column of the supplier table; this check constraint affects the city column only. When you issue an
INSERTorUPDATEoperation against the supplier table involving the city column, the SQL engine validates the column value, ensuring that the column does not contain the value 'Toxic Island'. If theINSERTorUPDATEstatement violates the check condition, the database returns a constraint violation error.
In Example 5–14 and Example 5–15, the
INSERTstatement results in an error, and the corresponding row is not inserted into the table.
Table-level check constraints
Your application might be required to enforce rules on multiple columns. To specify a constraint on more than one table column, define the constraint at the table level. For example, you might need to enforce a validation check on both the status and the city columns in the supplier table.
In Example 5–16, the table-level check constraint verifies that when the city is
CHICAGO, the status must be20, otherwise the operation returns a table-level check constraint violation.
Since the check constraint specification involves more than one column, you must specify it at the table level. If an
INSERTorUPDATEstatement violates the check condition, the database returns an error.Example 5–17 shows an
INSERTstatement for the supplier table created in the previous example. ThisINSERToperation results in a check constraint violation.
Primary keys
A primary key consists of one or more columns in a table that uniquely identifies each row. For example, the
supp_nocolumn value in the supplier table must be unique. Every row of the table is uniquely identified by this column value. A table can contain only one primary key constraint. If you supply a duplicate value for a primary key column in anINSERToperation, the operation returns an error.You can design your database table so that there is only one column that distinguishes a given row from other rows. In this case, a single column is the unique identifier of the table. For example, the
supp_nocolumn is a primary key for the supplier table. Primary key constraints are defined in the column definitions of a table.In Example 5–18, the
supp_nocolumn is a unique identifier in the supplier table, and the key consists of only one column. This example shows how to create a column-level primary key on the supplier table.
Candidate keys
If you design a table to require that a column or combination of columns define a row as unique, you define the columns with a candidate key constraint.
In Example 5–19, the employee number (
empno) is the primary key in the employee table because it uniquely identifies each row. Each entry in the employee social security column must also be distinct. Because a primary key has been designated already for the table, you must place a candidate key constraint on thess_nocolumn.
You declare a column as a candidate key by using the keyword
UNIQUE. Precede theUNIQUEkeyword with theNOTNULLspecification. Like a primary key, a candidate key also uniquely identifies a row in a table. Note that a table can have only one primary key, but can have any number of candidate keys.If you supply a duplicate value for a candidate key in an
INSERTorUPDATEoperation, the operation returns an error.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |